package com.ruoyi.app.controller.order.controller;

import com.ruoyi.app.controller.order.service.IMemberRechargeOrderService;
import com.ruoyi.app.controller.pay.service.IPayService;
import com.ruoyi.app.controller.shop.service.IShopAppletService;
import com.ruoyi.app.framework.base.BaseAppController;
import com.ruoyi.common.annotation.AuthMember;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.entity.DO.order.MemberRechargeOrderDO;
import com.ruoyi.common.entity.DO.shop.ShopAppletDO;
import com.ruoyi.common.entity.VO.PayVO;
import com.ruoyi.common.entity.VO.order.MemberRechargeOrderSearchVO;
import com.ruoyi.common.entity.wxpay.WxConfig;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/rechargeOrder")
@Api(value = "充值订单", description = "充值订单模块接口")
public class RechargeOrderController extends BaseAppController {

    @Resource
    private IMemberRechargeOrderService rechargeOrderService;

    @Resource
    private IShopAppletService shopAppletService;

    @Resource
    private IPayService payService;

    /**
     * 获取我的充值订单列表
     */
    @GetMapping("/searchList")
    public TableDataInfo searchList(@AuthMember MemberDO member) {
        MemberRechargeOrderSearchVO orderSearchVO = new MemberRechargeOrderSearchVO();
        orderSearchVO.setMemberId(member.getId());
        if (StringUtils.isNotEmpty(ServletUtils.getParameter("orderStatus"))) {
            orderSearchVO.setOrderStatus(ServletUtils.getParameterToInt("orderStatus"));
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("start", ServletUtils.getParameterToInt("start", 1));
        params.put("limits", ServletUtils.getParameterToInt("limits", 1000));
        params.put("orderBy", ServletUtils.getParameter("orderBy", "create_time desc"));
        BaseEntity pageVo = new BaseEntity();
        pageVo.setParams(params);
        startPage(pageVo);
        return getDataTable(rechargeOrderService.searchList(orderSearchVO));
    }


    /**
     * 创建充值订单
     * @param member
     * @param createVO
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@AuthMember MemberDO member, @Validated @RequestBody MemberRechargeOrderDO createVO) {
        //TODO：查询店铺的appId和secret
        if(null == createVO.getProductId()){
            throw new BusinessException("请选择充值金额！");
        }
        ShopAppletDO shopApplet = shopAppletService.getDetailByShopId(member.getShopId());
        if (shopApplet == null || shopApplet.getIsEnable() == null || shopApplet.getIsDelete() == 1) {
            throw new BusinessException("请联系运营平台重新开通小程序！");
        }
        if (shopApplet.getIsEnable() == 0) {
            throw new BusinessException("请联系运营平台重新上架小程序！");
        }
        //TODO：创建支付实体
        WxConfig wxConfig = new WxConfig();
        wxConfig.setAppID(shopApplet.getAppletAppId());
        wxConfig.setKey(shopApplet.getAppletPayKey());
        wxConfig.setMchID(shopApplet.getAppletMchId());
        try {
            wxConfig.setCertData(shopApplet.getShopId());
        } catch (Exception e) {
            error("支付证书错误！");
        }
        //TODO:创建充值订单获取支付实体
        createVO.setShopId(member.getShopId());
        createVO.setMemberId(member.getId());
        PayVO payInfo = rechargeOrderService.createOrder(createVO);
        if (payInfo == null) {
            return error("创建充值订单失败！");
        }
        payInfo.setOpenid(member.getOpenid());
        payInfo.setWxConfig(wxConfig);
        Map result = payService.memberRechargeOrderPay(payInfo);
        return AjaxResult.success(result);
    }
}
